﻿
using System.ComponentModel;
using System;
using System.Reflection;
namespace SharePointFarmInfoTimerJob
{
    enum ObjectType
    {
        Farm,
        Server,
        [DescriptionAttribute("SharePoint Service")]
        SharePointService,
    }

    /// <summary>
    /// Global
    /// </summary>
    class Global
    {
        #region Timerjob Info

        /// <summary>
        /// Timer job name
        /// </summary>
        public const string jobName = "SPFarmInfoTimerJob";
        /// <summary>
        /// Timer job title
        /// </summary>
        public const string jobTitle = "SharePoint Topology Data Collection Timer Job"; 

        #endregion

        #region Persited Object Key Names

        /// <summary>
        /// SPWebApplication Key name
        /// </summary>
        public const string SPWebApplication = "SPFarmInfoTimerJob_SPWEB";
        /// <summary>
        /// SPSiteCollection Key name
        /// </summary>
        public const string SPSiteCollection = "SPFarmInfoTimerJob_SPSite";
        /// <summary>
        /// SPSiteWebCollection Key name
        /// </summary>
        public const string SPSiteWebCollection = "SPFarmInfoTimerJob_SiteWEB";
        /// <summary>
        /// SPList Key name
        /// </summary>
        public const string SPList = "SPFarmInfoTimerJob_SPList";
       
        #endregion

        #region SPListInformation

        /// <summary>
        /// SPList Description
        /// </summary>
        public const string spListDescription = "This list contains SharePoint Farm servers and it's service information";
        /// <summary>
        /// SPList Columns
        /// </summary>
        public static string[] spListColumns = new string[] {
            "Index","ItemId","Object Type","Object Version","Config DB Name","Config DB Size in GB","Server Name", "Server Role", "Needs Upgrade","Can Upgrade","Service Name", "Status", "CPU Usage", 
            "System Directory Free space Details","System Directory Free space in GB", "System Directory Capacity in GB",
            "System Directory Free space in percent", "Memory Usage Details", "Free Memory in GB","Memory Capacity in GB","Memory Usage in Percent","Modified"
        };
        /// <summary>
        /// SPList Columns
        /// </summary>
        public static string[] spIntListColumns = new string[] {
            "Index", "Config DB Size in GB", "System Directory Free space in GB", "System Directory Capacity in GB","Free Memory in GB","Memory Capacity in GB"
        };
        /// <summary>
        /// SPList Columns
        /// </summary>
        public static string[] spIntListColumnsInPercentage = new string[] {
            "CPU Usage", "System Directory Free space in percent", "Memory Usage in Percent"
        };
        /// <summary>
        /// SPList Columns
        /// </summary>
        public static string[] spBoolListColumns = new string[] {
            "Needs Upgrade","Can Upgrade"
        };
        /// <summary>
        /// Server View Columns
        /// </summary>
        public static string[] FarmViewColumns = new string[] {
            "Index","ItemId","Object Type","Object Version","Config DB Name","Config DB Size in GB","Modified"
        };
        /// <summary>
        /// Server View Columns
        /// </summary>
        public static string[] ServerViewColumns = new string[] {
            "Index","ItemId","Object Type","Server Name", "Server Role","Object Version","Status", "Needs Upgrade","Can Upgrade", "CPU Usage", "System Directory Free space Details",
            "System Directory Free space in percent", "System Directory Free space in GB", "System Directory Capacity in GB","Memory Usage Details", "Memory Usage in Percent","Modified"
        };
        /// <summary>
        /// Service View Columns
        /// </summary>
        public static string[] ServiceViewColumns = new string[] {
            "Index","ItemId","Object Type","Server Name", "Service Name","Object Version","Status","Needs Upgrade","Can Upgrade","Modified"
        };
        /// <summary>
        /// "System Directory Free space in GB"
        /// </summary>
        public const string freeDiskSpace = "System Directory Free space in GB";
        /// <summary>
        /// "System Directory Capacity in GB"
        /// </summary>
        public const string diskSpaceCapacity = "System Directory Capacity in GB";
        /// <summary>
        /// "Free Memory in GB"
        /// </summary>
        public const string freeMemorySpace = "Free Memory in GB";
        /// <summary>
        /// "Memory Capacity in GB"
        /// </summary>
        public const string memoryCapacity = "Memory Capacity in GB";
        /// <summary>
        /// Modified
        /// </summary>
        public const string modified = "Modified";
        /// <summary>
        /// SharePoint Version
        /// </summary>
        public const string versionColumnName = "Object Version";
        /// <summary>
        /// Config DB Name
        /// </summary>
        public const string configDBNameColumnName = "Config DB Name";
        /// <summary>
        /// Config DB Size in GB
        /// </summary>
        public const string configDBSizeColumnName = "Config DB Size in GB";
        /// <summary>
        /// SharePoint Farm Server List View
        /// </summary>
        public const string sharePointFarmListView = "SharePoint Farm List View";
        /// <summary>
        /// SharePoint Farm Server List View
        /// </summary>
        public const string sharePointFarmServerListView = "SharePoint Farm Server List View";
        /// <summary>
        /// SharePoint Farm Service List View
        /// </summary>
        public const string sharePointFarmServiceListView = "SharePoint Farm Service List View";
        /// <summary>
        /// Can Upgrade
        /// </summary>
        public const string canUpgradeColumnName = "Can Upgrade";
        /// <summary>
        /// Needs Upgrade
        /// </summary>
        public const string needsUpgradeColumnName = "Needs Upgrade";
        /// <summary>
        /// Object Type
        /// </summary>
        public const string objectTypeColumnName = "Object Type";
        /// <summary>
        /// CPU Usage
        /// </summary>
        public const string cpuUsageColumnName = "CPU Usage";
        /// <summary>
        /// System Directory Free space Details
        /// </summary>
        public const string freeSpaceDetailsColumnName = "System Directory Free space Details";
        /// <summary>
        /// System Directory Free space in percent
        /// </summary>
        public const string freeSpacePercentColumnName = "System Directory Free space in percent";
        /// <summary>
        /// Memory Usage Details
        /// </summary>
        public const string memoryUsageDetailsColumnName = "Memory Usage Details";
        /// <summary>
        /// Memory Usage in Percent
        /// </summary>
        public const string memoryUsagePercentColumnName = "Memory Usage in Percent";
        /// <summary>
        /// Title column name
        /// </summary>
        public const string titleColumnName = "LinkTitle";
        /// <summary>
        /// List view query - Group and order by Server Name 
        /// </summary>
        public const string listViewQuery = @"<GroupBy Collapse=""TRUE""> <FieldRef Name=""Server_x0020_Name"" Ascending=""True""/> </GroupBy> <OrderBy> <FieldRef Name=""Server_x0020_Name"" /></OrderBy>";
        /// <summary>
        /// Custom List view query - Group and order by Server Name 
        /// </summary>
        public const string customListViewQuery = @"<Where><Eq><FieldRef Name='Object_x0020_Type'/><Value Type='Text'>{0}</Value></Eq></Where><GroupBy Collapse=""TRUE""> <FieldRef Name=""Server_x0020_Name"" Ascending=""True""/> </GroupBy> <OrderBy> <FieldRef Name=""Server_x0020_Name"" /></OrderBy>";
        /// <summary>
        /// Service id
        /// </summary>
        public const string uniqueid = "ItemId";
        /// <summary>
        /// List filter query to get the existing row.
        /// </summary>
        public const string listFilterQuery = @"<Where><Eq><FieldRef Name='ItemId'/><Value Type='Text'>{0}</Value></Eq></Where>";
        /// <summary>
        /// Index column name
        /// </summary>
        public const string indexColumnName = "Index";
        /// <summary>
        /// Server name
        /// </summary>
        public const string serverColumnName = "Server Name";
        /// <summary>
        /// Server role
        /// </summary>
        public const string serverRoleColumnName = "Server Role";
        /// <summary>
        /// Service name
        /// </summary>
        public const string serviceColumnName = "Service Name";
        /// <summary>
        /// Service status
        /// </summary>
        public const string statusColumnName = "Status";
        /// <summary>
        /// Service running status - running
        /// </summary>
        public const string serviceRunningStatus = "Running";
        /// <summary>
        /// Service stopped status - stopped
        /// </summary>
        public const string serviceStoppedStatus = "Stopped";
        #endregion

        #region WindowsTaskInfo

        //Win32_Processor WMI query
        public const string CPUUsageQuery = @"SELECT * FROM Win32_Processor";

        //Win32_OperatingSystem WMI query
        public const string WinOSQuery = @"SELECT * FROM Win32_OperatingSystem";

        //WMI management path
        public const string ManagementPath = "\\\\{0}\\root\\cimv2";

        //WMI Authentication failure error message
        public const string AuthenticationFailureErrorMessage = "Failed to authenticate properly";

        //WMI Unauthorized failure error message
        public const string UnauthorizedFailureMessage = "Service account is not authorized";

        //Generic failure error message
        public const string GenericFailureMessage = "Unknown error occured";

        //LoadPercentage - WMI Property
        public const string LoadPercentage = "LoadPercentage";

        //Percentage symbol
        public const string PercentageSymbol = "%";

        //Double quotes
        public const string DoubleQuotes = "\"";

        //Local machine
        public const string LocalMachineName = ".";

        //TotalVisibleMemorySize
        public const string TotalVisibleMemorySize = "TotalVisibleMemorySize";

        //FreePhysicalMemory
        public const string FreePhysicalMemory = "FreePhysicalMemory";

        //Memory usage string format
        public const string MemoryUsageStringFormat = "{0} of {1} GB";

        //FreeSpace
        public const string FreeSpace = "FreeSpace";

        //Size
        public const string Size = "Size";

        //DiskSpaceStringFormat
        public const string DiskSpaceStringFormat = "{0} GB free of {1} GB";

        //DeviceId
        public const string DeviceId = "win32_logicaldisk.deviceid=";

        //SystemDrive
        public const string SystemDrive = "SystemDrive";

        #endregion

        /// <summary>
        /// Convert Kilobyte to Gigabyte
        /// </summary>
        /// <param name="value">KB Value</param>
        /// <returns>GB Value</returns>
        public static double ConvertKBToGB(object value)
        {
            return Math.Round(Convert.ToDouble(value) / (1024 * 1024), 2);
        }

        /// <summary>
        /// Convert Byte to Gigabyte
        /// </summary>
        /// <param name="value">Byte Value</param>
        /// <returns>GB Value</returns>
        public static double ConvertBytesToGB(object value)
        {
            return Math.Round(Convert.ToDouble(value) / (1024 * 1024 * 1024), 2);
        }

        /// <summary>
        /// Get the description string of an Enum
        /// </summary>
        /// <param name="value">Enum value</param>
        /// <returns>Enum description string</returns>
        public static string stringValueOf(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                return attributes[0].Description;
            }
            else
            {
                return value.ToString();
            }
        }
    }
}
